import os.path
import time
from db.utils import Mysql
count = 0
def main():
    global  count
    mysql_instance = Mysql(host='127.0.0.1',user='root',password='',db='mydb')
    results = mysql_instance.query_table()

    img_dir = os.path.join(os.path.dirname(__file__),'uploads')
    if not os.path.exists(img_dir):
        os.mkdir(img_dir)
    for item in results:
        img_name = item['img_name'] + '.jpg'
        img_url = os.path.join(img_dir,img_name)
        with open(img_url,'wb') as f:
            f.write(item['img_data'])
            f.close()
            print(f'{img_name} ------ 写入成功')
            count += 1
if __name__ == '__main__':
    startTime = time.time()
    main()
    endTime = time.time()
    runTime = endTime - startTime
    print(f'用时{runTime}秒',f'共{count}张图片')


